/**
 * 
 */
package com.fresh.service.impl;

import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fresh.dao.PayRecordDao;
import com.fresh.dao.VerifyDao;
import com.fresh.entity.AntifraudUser;
import com.fresh.entity.ShopPrice;
import com.fresh.entity.UserPermissionEntity;
import com.fresh.entity.ZxExternalData;
import com.fresh.entity.ZxExternalSource;
import com.fresh.service.UserPermissionService;
import com.fresh.service.VerifyService;
import com.fresh.utils.PropertiesUtils;
import com.fresh.utils.Sequence;

/**
 * @author mengxiangdong E-mail: siyinmengzi@qq.com
 * @version 创建时间：2017年10月10日 上午9:27:11
 * 类说明
 */
/**
 * @author Administrator
 *
 */
@Service("verifyServiceImpl")
public class VerifyServiceImpl implements VerifyService {

	@Autowired
	private VerifyDao verifyDao;
	
	@Autowired
	private PayRecordDao payRecordDao;
	
	@Autowired
	private UserPermissionService userPermissionService;

	@Override
	public int save(String id,String json) {
		Timestamp now = new Timestamp(System.currentTimeMillis());
		
		ZxExternalSource zxExternalSource3 = new ZxExternalSource();
		zxExternalSource3.setId(Sequence.nextId());
		zxExternalSource3.setType(PropertiesUtils.getProperty("td_type"));
		zxExternalSource3.setVersion(PropertiesUtils.getProperty("td_version"));
		zxExternalSource3.setExternalId(id);
		zxExternalSource3.setDataSource(json);
		zxExternalSource3.setCreateTime(now);
		zxExternalSource3.setIntertype(Integer.valueOf(0));
		
		return verifyDao.saveSource(zxExternalSource3);
	}

	@Override
	public int saveSource(ZxExternalSource zxExternalSource) {
		return verifyDao.saveSource(zxExternalSource);
	}

	@Override
	public String saveAll(String resjdJson,
			String reshmdJson, AntifraudUser user, String shopId, String userId,int type) throws Exception {

		Timestamp now = new Timestamp(System.currentTimeMillis());

		// 接口数据入库处理
		ZxExternalData zxExternalData = new ZxExternalData();
		zxExternalData.setShopId(shopId);
		zxExternalData.setUserId(userId);
		zxExternalData.setId(Sequence.nextId());
		zxExternalData.setName(user.getName());
		zxExternalData.setIdNumber(user.getIdNumber());
		zxExternalData.setCardNumber(user.getCardNumber());
		zxExternalData.setPhone(user.getPhone());
		zxExternalData.setIpAddress(Sequence.getClientIpAddr());
		zxExternalData.setServiceType(Integer.valueOf(user.getDivision()));

		Calendar cc = Calendar.getInstance();
		cc.setTime(now);
		cc.add(5, 30);
		Timestamp tt2 = new Timestamp(cc.getTimeInMillis());
		zxExternalData.setValidTime(tt2);
		zxExternalData.setTimeApplication(now);
		zxExternalData.setCreateTime(now);
		zxExternalData.setDel(0);
		zxExternalData.setTotalNumber(1);
		verifyDao.save(zxExternalData);
	
		if (type==0) {
			ZxExternalSource zxExternalSource = new ZxExternalSource();
			zxExternalSource.setId(Sequence.nextId());
			zxExternalSource.setType(PropertiesUtils.getProperty("sm_type"));
			zxExternalSource.setVersion(PropertiesUtils.getProperty("sm_version"));
			zxExternalSource.setExternalId(zxExternalData.getId());
			zxExternalSource.setDataSource(resjdJson);
			zxExternalSource.setIntertype(Integer.valueOf(1));
			zxExternalSource.setCreateTime(now);
			verifyDao.saveSource(zxExternalSource);

			ZxExternalSource zxExternalSource2 = new ZxExternalSource();
			zxExternalSource2.setId(Sequence.nextId());
			zxExternalSource2.setType(PropertiesUtils.getProperty("sm_type"));
			zxExternalSource2.setVersion(PropertiesUtils.getProperty("sm_version"));
			zxExternalSource2.setExternalId(zxExternalData.getId());
			zxExternalSource2.setDataSource(reshmdJson);
			zxExternalSource2.setCreateTime(now);
			zxExternalSource2.setIntertype(Integer.valueOf(2));
			verifyDao.saveSource(zxExternalSource2);
		}
		
		return zxExternalData.getId();
	}

	@Override
	public void debit(HttpServletRequest request,String type) throws Exception {
	
		//扣款
	ZxExternalData zxc = new ZxExternalData();
	zxc.setShopId(getShopId(request));
    ZxExternalData zxcost = verifyDao.findcost(zxc);
    ShopPrice shopPrice=new ShopPrice();
	shopPrice.setShopId(getShopId(request));
	shopPrice.setType(type);  //反欺诈：0;评分卡：1......
	shopPrice=payRecordDao.findPrice(shopPrice);
	
	//可用余额
	if (shopPrice!=null && zxcost!=null) {
		BigDecimal balance=zxcost.getBalance().subtract(shopPrice.getPrice());
		//已用金额
		BigDecimal usedMoney=zxcost.getUsedMoney().add(shopPrice.getPrice());
		//修改商户金额数据
		zxc.setId(getShopId(request));
		zxc.setBalance(balance);
		zxc.setUsedMoney(usedMoney);
		payRecordDao.modify(zxc);
		
		//添加消费记录
		shopPrice.setId(Sequence.nextId());
		shopPrice.setCreateTime(new Date());
		payRecordDao.addPay(shopPrice);
	}

	}
	
	public   String getShopId( HttpServletRequest request )  {
		 Object object = request.getSession().getAttribute("SHOP_ID");
		 if(object ==null){
		 	return  null;
		}else{
			return  (String)object;
		}
	}

	/* (non-Javadoc)
	 * 权限拦截
	 * @see com.fresh.service.VerifyService#permissionIntercept(java.lang.String)
	 */
	@Override
	public void permissionIntercept(String permission,HttpServletResponse response,HttpServletRequest request) throws IOException {
		String userId=null;
		if (request.getSession().getAttribute("USER_ID")!=null) {
			userId = request.getSession().getAttribute("USER_ID").toString();  //用户id
		}else{
			 PrintWriter out = response.getWriter();  
		        out.println("<html>");      
		        out.println("<script>"); 
		        out.println("window.open ('"+request.getContextPath()+"/index','_top')");      
		        out.println("</script>");
		        out.println("</html>");
		}
		
       List<UserPermissionEntity> UserPermissionList = userPermissionService.getUserPermissionByUserId(userId);
		List<String> list=new ArrayList<String>();
		for (int i = 0; i < UserPermissionList.size(); i++) {
			UserPermissionEntity userPermissionEntity=UserPermissionList.get(i);
			String permissionType=userPermissionEntity.getType()+""+userPermissionEntity.getPermissionId();
			list.add(permissionType);
		}
		
		if (!list.contains(permission)) {
			PrintWriter out = response.getWriter();
	        out.println("<html>");
	        out.println("<script>");
	        out.println("window.open ('"+request.getContextPath()+"/index','_top')");      
	        out.println("</script>");
	        out.println("</html>");
		}
	}
	
	public static void main(String[] args) {
		List<String> list=new ArrayList<String>();
		list.add("01");
		list.add("02");
		list.add("12");
		list.add("11");
		System.out.println(list.contains("05"));
	}
}
